package com.csdj.order.mq;

import org.apache.rocketmq.client.producer.LocalTransactionState;
import org.apache.rocketmq.client.producer.TransactionListener;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 事务消息监听器
 */
@Component
public class OrderMsgListener implements TransactionListener {

    private Logger log = LoggerFactory.getLogger("TransactionListener");

    /**
     * 消息确认或是回滚
     * @param msg
     * @param arg
     * @return
     */
    @Override
    public LocalTransactionState executeLocalTransaction(Message msg, Object arg) {

        //事务id
        String tid = msg.getTransactionId();
        //key
        String keys = msg.getKeys();
        //要操作的业务代码
        log.info("消息确认 tid:{}  keyss:{}",tid,keys);
        //确认消息
//        return LocalTransactionState.COMMIT_MESSAGE;
        //回滚消息
        return LocalTransactionState.ROLLBACK_MESSAGE;

    }

    /**
     * 消息回查
     * @param msg
     * @return
     */
    @Override
    public LocalTransactionState checkLocalTransaction(MessageExt msg) {
        final String tid = msg.getTransactionId();
        final String keys = msg.getKeys();
        log.info("rocket 消息回查， tid:{}  keys:{}",tid,keys);
        return LocalTransactionState.COMMIT_MESSAGE;
    }
}
